package com.isoft.filelistener.flistener;

import com.isoft.filelistener.Utils.ApplicationContextProvider;
import com.isoft.filelistener.Utils.XmlUtils;
import com.isoft.filelistener.entity.*;
import com.isoft.filelistener.service.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.monitor.FileAlterationListener;
import org.apache.commons.io.monitor.FileAlterationObserver;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.text.SimpleDateFormat;

@Component
@Slf4j
public class FileListener implements FileAlterationListener {
    private  String driction=null;
    @Override
    public void onDirectoryChange(File file) {

        log.info("文件夹修改。。。。。");
    }

    @Override
    public void onDirectoryCreate(File file) {
        driction=file.getName();
        log.info("文件夹创建。。。。");
    }

    @Override
    public void onDirectoryDelete(File file) {

        log.info("文件夹删除。。。。");
    }

    @Override
    public void onFileChange(File file) {

        log.info("文件修改。。。。");
    }

    @Override
    public void onFileCreate(File file) {
        String name= file.getName();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSSS");
        String id =sdf.format(System.currentTimeMillis());
        switch (name.substring(0,9)){
            case "DFME-BORE":
                try {
                    //创建输入流
                    InputStream inputStream = new FileInputStream("D:\\files\\"+driction+"\\"+file.getName());
                    log.info(inputStream + "");
                    DfmeBore dfmeBore = XmlUtils.parseXml(inputStream, DfmeBore.class);  //将xml文件转化为java对象(Msg类的实例)
                    ApplicationContextProvider.getBean(DfmeBoreService.class).insertbore(dfmeBore.getMeta(),dfmeBore.getBoreDflt());
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
                break;
            case "DFME-CANE":
                try {
                    //创建输入流
                    InputStream inputStream = new FileInputStream("D:\\files\\"+driction+"\\"+file.getName());
                    log.info(inputStream + "");
                    DfmeCane dfmeCane = XmlUtils.parseXml(inputStream, DfmeCane.class);  //将xml文件转化为java对象(Msg类的实例)
                    ApplicationContextProvider.getBean(DfmeCaneService.class).insertcane(dfmeCane.getMeta(), dfmeCane.getCaneDflt());
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
                break;
            case "DFME-CFCE":
                try {
                    //创建输入流
                    InputStream inputStream = new FileInputStream("D:\\files\\"+driction+"\\"+file.getName());
                    log.info(inputStream + "");
                    DfmeCfce dfmeCfce = XmlUtils.parseXml(inputStream, DfmeCfce.class);  //将xml文件转化为java对象(Msg类的实例)
                    ApplicationContextProvider.getBean(DfmeCfceService.class).insertcfce(dfmeCfce.getMeta(), dfmeCfce.getCfceDflt());
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
                break;
            case "DFME-CKIE":
                try {
                    //创建输入流
                    InputStream inputStream = new FileInputStream("D:\\files\\"+driction+"\\"+file.getName());
                    log.info(inputStream + "");
                    DfmeCkie dfmeCkie = XmlUtils.parseXml(inputStream, DfmeCkie.class);  //将xml文件转化为java对象(Msg类的实例)
                    ApplicationContextProvider.getBean(DfmeCkieService.class).insertckie(dfmeCkie.getMeta(), dfmeCkie.getCkieDflt());
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
                break;
            case "DFME-CKLS":
                try {
                    InputStream inputStream=new FileInputStream("D:\\files\\"+driction+"\\"+file.getName());
                    log.info(inputStream+"");
                    DfmeCkls dfmeCkls = XmlUtils.parseXml(inputStream, DfmeCkls.class);
                    //meta头部插入语句
                    ApplicationContextProvider.getBean(DfmeCklsService.class).InsertDfmeMeta(dfmeCkls.getMetaData(),id);
                    //dflt插入语句
                    ApplicationContextProvider.getBean(DfmeCklsService.class).InsertDfmeDflt(dfmeCkls.getDflt(),id);
                    //ckls插入语句
                    ApplicationContextProvider.getBean(DfmeCklsService.class).InsertDfmeCkls(dfmeCkls.getDflt().getCkls(), id);
                    //cenr插入语句
                    for (int i = 0; i < dfmeCkls.getDflt().getCkls().getCntr().size(); i++) {
                        DfmeCklsDfltCklsCntr dfmeCklsDfltCklsCntr = dfmeCkls.getDflt().getCkls().getCntr().get(i);
                        ApplicationContextProvider.getBean(DfmeCklsService.class).InsertDfmeCntr(dfmeCklsDfltCklsCntr,id);
                    }

                } catch (Exception ex) {
                    ex.printStackTrace();
                }
                break;
            case "DFME-CKOE":
                try {
                    //创建输入流
                    InputStream inputStream = new FileInputStream("D:\\files\\"+driction+"\\"+file.getName());
                    DfmeCkoe dfmeCkoe = XmlUtils.parseXml(inputStream, DfmeCkoe.class);  //将xml文件转化为java对象(Msg类的实例)
                    ApplicationContextProvider.getBean(DfmeCkoeService.class).insertckoe(dfmeCkoe.getMeta(),dfmeCkoe.getCkoeDflt());
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
                break;
            default:
                log.info("没有此文件");
        }
    }

    @Override
    public void onFileDelete(File file) {

        log.info("文件删除。。。。:"+file.getName());
    }

    @Override
    public void onStart(FileAlterationObserver fileAlterationObserver) {
        log.info("监听开始1。。。。");
    }

    @Override
    public void onStop(FileAlterationObserver fileAlterationObserver) {
        log.info("监听结束1。。。。");
    }
}
